// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyViewerClientInformationElement.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PyViewerClientInformationElement
//
// Purpose:
//   This class contains the raw element
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a ViewerClientInformationElement.
//
struct ViewerClientInformationElementObject
{
    PyObject_HEAD
    ViewerClientInformationElement *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewViewerClientInformationElement(int);
std::string
PyViewerClientInformationElement_ToString(const ViewerClientInformationElement *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    snprintf(tmpStr, 1000, "%sdata = \"%s\"\n", prefix, atts->GetData().c_str());
    str += tmpStr;
    {   const unsignedCharVector &rawData = atts->GetRawData();
        snprintf(tmpStr, 1000, "%srawData = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < rawData.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", int(rawData[i]));
            str += tmpStr;
            if(i < rawData.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    snprintf(tmpStr, 1000, "%sformat = %d\n", prefix, atts->GetFormat());
    str += tmpStr;
    if(atts->GetIsRaw())
        snprintf(tmpStr, 1000, "%sisRaw = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sisRaw = 0\n", prefix);
    str += tmpStr;
    snprintf(tmpStr, 1000, "%swindowId = %d\n", prefix, atts->GetWindowId());
    str += tmpStr;
    return str;
}

static PyObject *
ViewerClientInformationElement_Notify(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ViewerClientInformationElement_SetData(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the data in the object.
    obj->data->SetData(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ViewerClientInformationElement_GetData(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetData().c_str());
    return retval;
}

/*static*/ PyObject *
ViewerClientInformationElement_SetRawData(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;

    typedef unsigned char uchar;
    ucharVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        uchar cval = uchar(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ uchar");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ uchar");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            uchar cval = uchar(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ uchar", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ uchar", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more uchars");

    obj->data->GetRawData() = vec;
    // Mark the rawData in the object as modified.
    obj->data->SelectRawData();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ViewerClientInformationElement_GetRawData(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;
    // Allocate a tuple the with enough entries to hold the rawData.
    const unsignedCharVector &rawData = obj->data->GetRawData();
    PyObject *retval = PyTuple_New(rawData.size());
    for(size_t i = 0; i < rawData.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(rawData[i])));
    return retval;
}

/*static*/ PyObject *
ViewerClientInformationElement_SetFormat(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ int");
    }

    Py_XDECREF(packaged_args);

    // Set the format in the object.
    obj->data->SetFormat(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ViewerClientInformationElement_GetFormat(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetFormat()));
    return retval;
}

/*static*/ PyObject *
ViewerClientInformationElement_SetIsRaw(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the isRaw in the object.
    obj->data->SetIsRaw(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ViewerClientInformationElement_GetIsRaw(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetIsRaw()?1L:0L);
    return retval;
}

/*static*/ PyObject *
ViewerClientInformationElement_SetWindowId(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ int");
    }

    Py_XDECREF(packaged_args);

    // Set the windowId in the object.
    obj->data->SetWindowId(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ViewerClientInformationElement_GetWindowId(PyObject *self, PyObject *args)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetWindowId()));
    return retval;
}



PyMethodDef PyViewerClientInformationElement_methods[VIEWERCLIENTINFORMATIONELEMENT_NMETH] = {
    {"Notify", ViewerClientInformationElement_Notify, METH_VARARGS},
    {"SetData", ViewerClientInformationElement_SetData, METH_VARARGS},
    {"GetData", ViewerClientInformationElement_GetData, METH_VARARGS},
    {"SetRawData", ViewerClientInformationElement_SetRawData, METH_VARARGS},
    {"GetRawData", ViewerClientInformationElement_GetRawData, METH_VARARGS},
    {"SetFormat", ViewerClientInformationElement_SetFormat, METH_VARARGS},
    {"GetFormat", ViewerClientInformationElement_GetFormat, METH_VARARGS},
    {"SetIsRaw", ViewerClientInformationElement_SetIsRaw, METH_VARARGS},
    {"GetIsRaw", ViewerClientInformationElement_GetIsRaw, METH_VARARGS},
    {"SetWindowId", ViewerClientInformationElement_SetWindowId, METH_VARARGS},
    {"GetWindowId", ViewerClientInformationElement_GetWindowId, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
ViewerClientInformationElement_dealloc(PyObject *v)
{
   ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *ViewerClientInformationElement_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyViewerClientInformationElement_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "data") == 0)
        return ViewerClientInformationElement_GetData(self, NULL);
    if(strcmp(name, "rawData") == 0)
        return ViewerClientInformationElement_GetRawData(self, NULL);
    if(strcmp(name, "format") == 0)
        return ViewerClientInformationElement_GetFormat(self, NULL);
    if(strcmp(name, "isRaw") == 0)
        return ViewerClientInformationElement_GetIsRaw(self, NULL);
    if(strcmp(name, "windowId") == 0)
        return ViewerClientInformationElement_GetWindowId(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyViewerClientInformationElement_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyViewerClientInformationElement_methods[i].ml_name),
                PyString_FromString(PyViewerClientInformationElement_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyViewerClientInformationElement_methods, self, name);
}

int
PyViewerClientInformationElement_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "data") == 0)
        obj = ViewerClientInformationElement_SetData(self, args);
    else if(strcmp(name, "rawData") == 0)
        obj = ViewerClientInformationElement_SetRawData(self, args);
    else if(strcmp(name, "format") == 0)
        obj = ViewerClientInformationElement_SetFormat(self, args);
    else if(strcmp(name, "isRaw") == 0)
        obj = ViewerClientInformationElement_SetIsRaw(self, args);
    else if(strcmp(name, "windowId") == 0)
        obj = ViewerClientInformationElement_SetWindowId(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
ViewerClientInformationElement_print(PyObject *v, FILE *fp, int flags)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)v;
    fprintf(fp, "%s", PyViewerClientInformationElement_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
ViewerClientInformationElement_str(PyObject *v)
{
    ViewerClientInformationElementObject *obj = (ViewerClientInformationElementObject *)v;
    return PyString_FromString(PyViewerClientInformationElement_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *ViewerClientInformationElement_Purpose = "This class contains the raw element";
#else
static char *ViewerClientInformationElement_Purpose = "This class contains the raw element";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(ViewerClientInformationElementType,         \
                  "ViewerClientInformationElement",           \
                  ViewerClientInformationElementObject,       \
                  ViewerClientInformationElement_dealloc,     \
                  ViewerClientInformationElement_print,       \
                  PyViewerClientInformationElement_getattr,   \
                  PyViewerClientInformationElement_setattr,   \
                  ViewerClientInformationElement_str,         \
                  ViewerClientInformationElement_Purpose,     \
                  ViewerClientInformationElement_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
ViewerClientInformationElement_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &ViewerClientInformationElementType
         || Py_TYPE(other) != &ViewerClientInformationElementType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    ViewerClientInformationElement *a = ((ViewerClientInformationElementObject *)self)->data;
    ViewerClientInformationElement *b = ((ViewerClientInformationElementObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static ViewerClientInformationElement *defaultAtts = 0;
static ViewerClientInformationElement *currentAtts = 0;

static PyObject *
NewViewerClientInformationElement(int useCurrent)
{
    ViewerClientInformationElementObject *newObject;
    newObject = PyObject_NEW(ViewerClientInformationElementObject, &ViewerClientInformationElementType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new ViewerClientInformationElement(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new ViewerClientInformationElement(*defaultAtts);
    else
        newObject->data = new ViewerClientInformationElement;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapViewerClientInformationElement(const ViewerClientInformationElement *attr)
{
    ViewerClientInformationElementObject *newObject;
    newObject = PyObject_NEW(ViewerClientInformationElementObject, &ViewerClientInformationElementType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (ViewerClientInformationElement *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
ViewerClientInformationElement_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewViewerClientInformationElement(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef ViewerClientInformationElementMethods[] = {
    {"ViewerClientInformationElement", ViewerClientInformationElement_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *ViewerClientInformationElementObserver = 0;

std::string
PyViewerClientInformationElement_GetLogString()
{
    std::string s("ViewerClientInformationElement = ViewerClientInformationElement()\n");
    if(currentAtts != 0)
        s += PyViewerClientInformationElement_ToString(currentAtts, "ViewerClientInformationElement.", true);
    return s;
}

static void
PyViewerClientInformationElement_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("ViewerClientInformationElement = ViewerClientInformationElement()\n");
        s += PyViewerClientInformationElement_ToString(currentAtts, "ViewerClientInformationElement.", true);
        cb(s);
    }
}

void
PyViewerClientInformationElement_StartUp(ViewerClientInformationElement *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyViewerClientInformationElement_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(ViewerClientInformationElementObserver == 0)
    {
        ViewerClientInformationElementObserver = new ObserverToCallback(subj,
            PyViewerClientInformationElement_CallLogRoutine, (void *)data);
    }

}

void
PyViewerClientInformationElement_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete ViewerClientInformationElementObserver;
    ViewerClientInformationElementObserver = 0;
}

PyMethodDef *
PyViewerClientInformationElement_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return ViewerClientInformationElementMethods;
}

bool
PyViewerClientInformationElement_Check(PyObject *obj)
{
    return (obj->ob_type == &ViewerClientInformationElementType);
}

ViewerClientInformationElement *
PyViewerClientInformationElement_FromPyObject(PyObject *obj)
{
    ViewerClientInformationElementObject *obj2 = (ViewerClientInformationElementObject *)obj;
    return obj2->data;
}

PyObject *
PyViewerClientInformationElement_New()
{
    return NewViewerClientInformationElement(0);
}

PyObject *
PyViewerClientInformationElement_Wrap(const ViewerClientInformationElement *attr)
{
    return WrapViewerClientInformationElement(attr);
}

void
PyViewerClientInformationElement_SetParent(PyObject *obj, PyObject *parent)
{
    ViewerClientInformationElementObject *obj2 = (ViewerClientInformationElementObject *)obj;
    obj2->parent = parent;
}

void
PyViewerClientInformationElement_SetDefaults(const ViewerClientInformationElement *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new ViewerClientInformationElement(*atts);
}

